function AddFields()
{
	// build argument list to pass to dialog
	//
	var oArgs = new Object();
	oArgs.FieldsXml				= oFieldsXml;
	oArgs.FieldPropertiesXml	= oPropertiesXml;
	oArgs.PreviewXml			= oPreviewXml;
	oArgs.Spaces				= GetAvailableSpaces();
	
	var oFieldObjs = openStdDlg("/Tools/PreviewManager/Dialogs/addfields.aspx", oArgs, 550, 450);
	
	if (oFieldObjs)
	{
		var i;
		var iLen = oFieldObjs.length;
		
		for (i = 0; i < iLen; i++)
		{
			AddFieldPreviewXml(oFieldObjs[i]);
			AddFieldHtml(oFieldObjs[i]);
		}
	}
}

function GetAvailableSpaces()
{
	var oRows= oPreviewXml.selectNodes("/preview/rows/row");
	var oCells, oRow, oCell;
	var total = 0;
	
	for (var i=0; i< oRows.length; i++)
	{
		oRow = oRows.item(i);
		oCells = oRow.selectNodes("cell");
		
		if (oCells.length == 1) //spans both columns
		{
			total = total + 2;
		}
		else if (oCells)
		{
			if (oCells.item(0).getAttribute("name"))
			{
				total ++;
			}
			if (oCells.item(1).getAttribute("name"))
			{
				total ++;
			}
		}
	}
	return _MaxFields-total;
}

function GetAvailableRows()
{
	var oRows= oPreviewXml.selectNodes("/preview/rows/row");
	
	return _MaxRows-oRows.length;
}

function AddFieldPreviewXml(oFieldObj)
{
	var oRows = GetRowsNode();
	
	var i;
	var ii;
	
	var oRow;
	var oCell;
	
	var iRowsLen = oRows.childNodes.length;
	var iRowLen;
	
	// loop through each row and cell and if there's an empty one use it for this field
	//
	for (i = 0; i < iRowsLen; i++)
	{
		oRow	= oRows.childNodes[i];
		iRowLen	= oRow.childNodes.length;
		
		for (ii = 0; ii < iRowLen; ii++)
		{
			oCell = oRow.childNodes[ii];
			
			if (!oCell.getAttribute("name"))
			{
				oCell.setAttribute("name", oFieldObj.FieldName);
				return;
			}
		}
	}
	
	// you'll only make it this far if there were no empty cells
	
	oRow	= oPreviewXml.createElement("row");

	oRows.appendChild(oRow);
	
	oCell	= oPreviewXml.createElement("cell");

	oCell.setAttribute("name", oFieldObj.FieldName);

	oRow.appendChild(oCell);
				
	oCell	= oPreviewXml.createElement("cell");

	oRow.appendChild(oCell);
}


function AddFieldHtml(oFieldObj)
{
	var i,j;
	var oRow;
	var iRowsLen = rowsContent.rows.length;
	var oCell;
	var iRowLen;
	
	// loop through each row and cell and if there's an empty one use it for this field
	//
	for (i = 0; i < iRowsLen; i++)
	{
		oRow	= rowsContent.rows[i];
		
		iRowLen	= oRow.cells.length;
		
		for (j = 0; j < iRowLen; j++)
		{
			oCell = oRow.cells[j];
			
			if (!oCell.name)
			{
				oRow.deleteCell(j);
				InsertCellToDom(oRow,oFieldObj,j);

				return;
			}
		}
	}
	
	// you'll only make it this far if there were no empty cells
	var oRow	= rowsContent.insertRow();
	
	InsertCellToDom(oRow,oFieldObj,0);
	InsertCellToDom(oRow,null,1);

}

function InsertCellToDom(oRow,oFieldObj,cellIndex)
{
	var fieldDesc="";
	var fieldName="";
	var sColor = "#000000";
	var newTD;
	
	if (oFieldObj)
	{
		fieldDesc = GetDescByLangCode(oFieldObj.DisplayNames);
		fieldName = oFieldObj.FieldName;
		
		newTD = document.createElement("<td class='cell' name='"+ fieldName +"' onclick='SetActiveObject();' ondblclick='ViewPropertiesNow();'></td>");
		newTD.innerHTML = "<table cellspacing='0' cellpadding='0' style='table-layout: fixed;'><col /><tr height='18'><td nowrap class='label'>"+ fieldDesc +" :</td></tr></table>";
	}
	else
	{
		newTD = document.createElement("<td class='cell'></td>");
	}

	oRow.insertBefore(newTD,oRow.cells(cellIndex));
	newTD.click();

}


function UpdateField(oActive)
{
	var sFieldName		= oActive.name;
	
	var oEntityField		= oFieldsXml.selectSingleNode("/entity/fields/field[@name = '" + sFieldName + "']");
	var oPropertiesField	= oPropertiesXml.selectSingleNode("/entity/fields/field[@name = '" + sFieldName + "']");
	var oPreviewCell		= GetCellNode(sFieldName);
	
	var sDataType		= oEntityField.getAttribute("datatype");
	var bSpan			= (oActive.parentElement.cells.length == 1);
	var oDisplayNames	= GetLocalizedObjsArray(oPropertiesField.selectNodes("displaynames/displayname"));
		
	var oFieldObj = new FieldObj(sFieldName, sDataType, null, null, bSpan, oDisplayNames, null)
	
	var Spaces			= GetAvailableSpaces();
	var oArgs			= new Object();

	oArgs.Field			= oFieldObj;
	oArgs.PreviewXml	= oPreviewXml;
	oArgs.Spaces		= Spaces;

	var sUrl = "/Tools/PreviewManager/Dialogs/fieldProperties.aspx";
	var STANDARD_MODAL_FEATURES	= "dialogHeight:350px;dialogWidth:350px;center:yes;status:no;help:no;scroll:no;";

	var oUpdatedFieldObj = window.showModalDialog(sUrl, oArgs, STANDARD_MODAL_FEATURES);
	
	if (oUpdatedFieldObj)
	{
		// if the span changed adjust it
		//
		if (oFieldObj.Span != oUpdatedFieldObj.Span)
		{
			var Rows = GetAvailableRows();
			
			if ( (oUpdatedFieldObj.Span) && (_MaxRows + Rows == _MaxRows) && (_MaxFields + Spaces != _MaxFields) )//span both and no more rows but empty space
			{
				SetFieldSpan(oUpdatedFieldObj.FieldName, oUpdatedFieldObj.Span, oActive, true);
			}
			else
			{
				SetFieldSpan(oUpdatedFieldObj.FieldName, oUpdatedFieldObj.Span, oActive, false);
			}
		}
	}
}

function FindSiblingXml(oNode)
{
	if (oNode.nextSibling)
	{
		return oNode.nextSibling;
	}
	else
	{
		return oNode.previousSibling;
	}
}

function FindEmptyCellXml()
{	
	var oRows = oPreviewXml.selectNodes("/preview/rows/row/cell[not(@name)]");
	return oRows.item(0);
}


function ReplaceEmptyCellXml(EmptyCell,SiblingCell)
{
	var SiblingParent = SiblingCell.parentNode;
	var EmptyParent	= EmptyCell.parentNode;
	
	if (EmptyParent != SiblingParent)
	{
		EmptyParent.replaceChild(SiblingParent.removeChild(SiblingCell),EmptyCell);
	}
	else
	{
		SiblingParent.removeChild(SiblingCell);
	}

}

function FindSiblingHtml(oNode)
{
	if (oNode.nextSibling)
	{
		return oNode.nextSibling;
	}
	else
	{
		return oNode.previousSibling;
	}
}

function FindEmptyCellHtml()
{	
	var i;
	var oRow;
	var iRowsLen = rowsContent.rows.length;

	for (i = 0; i < iRowsLen; i++)
	{
		oRow = rowsContent.rows[i];
		
		// if we find one, strip it and jump out since there will never be more than one
		//
		if (oRow.cells[0].innerHTML == "") 
		{
			return oRow.cells[0];
		}
		else if (oRow.cells[1]) //if there is more than one column
		{	
			if (oRow.cells[1].innerHTML == "") 
			{
				return oRow.cells[1];
			}
		}
	}
	return null;
}


function ReplaceEmptyCellHtml(EmptyCell,SiblingCell)
{
	SiblingCell.swapNode(EmptyCell);
}

function SpanCellHtml(Node)
{	
	var oParentElem	= Node.parentElement;
	var index = (Node.cellIndex==1) ? 0 : 1;

	oParentElem.deleteCell(index);
	Node.colSpan = 2;
}
		
function SetFieldSpan(FieldName, Span, oActive, autoMode)
{
	var oRow;
	var oCell;	

	if (Span) //expand both columns
	{
		oCell = GetCellNode(FieldName);
		var oNextCell	= oCell.nextSibling;
		var oParentNode	= oCell.parentNode;
		var oParentElem	= oActive.parentElement;
		var oSibling;
	
		if (autoMode) //find first empty space
		{
			ReplaceEmptyCellXml(FindEmptyCellXml(),FindSiblingXml(oCell));
			ReplaceEmptyCellHtml(FindEmptyCellHtml(),FindSiblingHtml(oActive));
			SpanCellHtml(oActive);
		}
		else
		{
			if (oNextCell)
			{
				if (oNextCell.getAttribute("name"))
				{
					// update xml
					oRow = oPreviewXml.createElement("row");
					oRow.appendChild(GetEmptyCellNode());
					oRow.appendChild(oNextCell);
					oParentNode.parentNode.insertBefore(oRow, oParentNode.nextSibling);
					
					// update html
					oRow = oParentElem.parentElement.insertRow(oParentElem.rowIndex + 1);			
					oRow.insertCell();
					oRow.insertCell();
					oActive.nextSibling.swapNode(oRow.cells[1]);
					oParentElem.deleteCell(1);
					oActive.colSpan = 2;
				}
				else
				{
					// update xml
					oParentNode.removeChild(oNextCell);
					
					// update html
					oParentElem.deleteCell(1);
					oActive.colSpan = 2;
				}
			}
			else
			{
				var oPrevCell = oCell.previousSibling;
				if (oPrevCell.getAttribute("name"))
				{
					// update xml
					oRow = oPreviewXml.createElement("row");
					oRow.appendChild(oPrevCell);
					oRow.appendChild(GetEmptyCellNode());
					oParentNode.parentNode.insertBefore(oRow, oParentNode.nextSibling);
					
					// update html
					oRow = oParentElem.parentElement.insertRow(oParentElem.rowIndex + 1);					
					oRow.insertCell();
					oRow.insertCell();
					
					oActive.previousSibling.swapNode(oRow.cells[0]);
					oParentElem.deleteCell(0);
					oActive.colSpan = 2;
				}
				else
				{
					// update xml
					oParentNode.removeChild(oPrevCell);
					
					// update html
					oActive.parentElement.deleteCell(0);
					oActive.colSpan = 2;
				}
			}
		}
	}
	else //expand one column
	{
		// update xml
		//
		oRow = GetRowNode(FieldName)
		
		oRow.appendChild(GetEmptyCellNode());

		// update html
		//
		oActive.colSpan = 1;
		
		oActive.parentElement.insertCell();
	}
}


function RemoveField(oActive)
{
	//Remove Field from preview Xml
	var oCell = GetCellNode(oActive.name);
	oCell.removeAttribute("name");

	// Remove Field from Html dom
	var oRow	= oActive.parentElement;
	var iIndex	= oActive.cellIndex;
	oRow.deleteCell(iIndex);
	oRow.insertCell(iIndex);

	PurgeEmptyRowsXml();
	PurgeEmptyRowsHtml();
	
	SetNoActiveObject();
}
